using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace WebPartZoneAdapter
{
    class WebPartZoneAdapterFeature : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;

            SPWebApplication webApplication = site.WebApplication;

            foreach (SPUrlZone zone in webApplication.IisSettings.Keys)
            {
                // The settings of the IIS application to update
                SPIisSettings settings = webApplication.IisSettings[zone];

                string basePath = settings.Path.ToString();
                string destPath = Path.Combine(basePath, "App_Browsers");
                System.IO.FileStream file = new System.IO.FileStream(Path.Combine(destPath, "WebPartZoneAdapter.browser") , FileMode.Create , FileAccess.Write);
                System.IO.StreamWriter writer = new System.IO.StreamWriter(file);
                System.IO.Stream resource = this.GetType().Assembly.GetManifestResourceStream("WebPartZoneAdapter.FeatureCode.WebPartZoneAdapter.browser");
                System.IO.StreamReader reader = new System.IO.StreamReader(resource);
                string s = reader.ReadToEnd();
                s = s.Replace("{707D9AE2-D892-448f-8DC4-4BBC2C95770D}", typeof(WebPartZone_Adapter).FullName + ", " + typeof(WebPartZone_Adapter).Assembly.FullName);
                writer.Write(s);
                writer.Flush();
                writer.Close();
                reader.Close();

                // Touch .browser-files to force activation of the control adapter
                foreach (string filename in System.IO.Directory.GetFiles(destPath , "*.browser"))
                {
                    System.IO.File.SetLastWriteTime(filename, DateTime.Now);
                }
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;
            SPWebApplication webApplication = site.WebApplication;

            foreach (SPUrlZone zone in webApplication.IisSettings.Keys)
            {
                // The settings of the IIS application to update
                SPIisSettings settings = webApplication.IisSettings[zone];

                string basePath = settings.Path.ToString();
                string destPath = Path.Combine(basePath, "App_Browsers");

                if (System.IO.File.Exists(Path.Combine(destPath, "WebPartZoneAdapter.browser")))
                {
                    System.IO.File.Delete(Path.Combine(destPath, "WebPartZoneAdapter.browser"));
                }

                // Touch .browser-files to force de-activation of the control adapter
                foreach (string filename in System.IO.Directory.GetFiles(destPath, "*.browser"))
                {
                    System.IO.File.SetLastWriteTime(filename, DateTime.Now);
                }

            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}
